//
//  SeparatePlanViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "SeparatePlanViewController.h"
#import "SeparateDetailsViewController.h"

#import "SeparatePlanCell.h"
#import "MeatPayView.h"



@interface SeparatePlanViewController ()

@property (nonatomic, strong) MeatPayView *footer;

@property (nonatomic , strong) UIButton *detailTypeBtn;

@property (nonatomic , strong) NSMutableArray *transicationTypeArr;

@property (nonatomic , assign) TransicationType transicationType;

@property (nonatomic, strong) ShopSeparateModel *model;



@end

@implementation SeparatePlanViewController

- (void)setupUI{
    
    {
        self.baseNavView.navTitleLabel.text = @"分配方案";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        UIImage *image = [UIImage imageNamed:@"meat_question_nav"];
        self.baseNavView.layer.contents =  (__bridge id)image.CGImage;
    }
    
    {
        [self.baseNavView.rightNavBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [self.baseNavView.rightNavBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        self.baseNavView.rightNavBtn.isNeedAnimation = YES;
        
        __block BOOL selected = NO;
        @weakify(self);
        self.baseNavView.rightNavBtn.tapBlock = ^{
            @strongify(self);
            if (selected) {
                selected = NO;
                [self.baseNavView.rightNavBtn setTitle:@"编辑" forState:UIControlStateNormal];
                self.footer.alpha = 0;
            }else{
                selected = YES;
                [self.baseNavView.rightNavBtn setTitle:@"完成" forState:UIControlStateNormal];
                self.footer.alpha = 1;
            }
        };
        
        
        self.detailTypeBtn.mj_y = self.baseNavView.bottom;
        self.detailTypeBtn.mj_x = 15;
        self.detailTypeBtn.height = 34;
        [self.view addSubview:self.detailTypeBtn];
    }

    {
        [self.view addSubview:self.footer];
    }
    
}


- (void)setupLayout{
    
    {
        self.reloadBtn.mj_y += 30;
        self.reloadLab.mj_y += 30;
    }
    
    {
        self.footer.mj_y = self.view.bottom - _footer.height;
    }
}


#pragma mark - networking methods
//分配方案（肉块分配 Yes
- (void)initData{
    
    NSArray *titleArr = @[@"全部",@"预售款",@"充值",@"养猪",@"买肉",@"提现",@"奖金"];
    
    for (int i = 0; i < 7; i ++) {
        PullDownMenuModel *model = [[PullDownMenuModel alloc] init];
        model.title = titleArr[i];
        model.isSelect = NO;
        [self.transicationTypeArr addObject:model];
    };
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopSeparateParam *param = [ShopSeparateParam new];
    param.shopId = @0;
    param.pigId = @0;
    param.pigPositionId = @0;
    param.page = page;
    param.limit = @10;
    [self requestSeparatePlanListByParam:param];
    
}

- (void)requestSeparatePlanListByParam:(ShopSeparateParam *)param{
    
    @weakify(self);
    [[[MeatShopApi sharedInstance] getShopSparateByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable model) {
        @strongify(self);
        
        self.model = model;
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if ([self.model.page.totalCount intValue] < [self.model.page.pageSize intValue] ) {
            [self endLoadMoreMoData];
        }else if ([self.model.page.totalCount intValue] == [self.model.page.pageSize intValue]){
            
            if ([self.model.page.currPage intValue] >= [self.model.page.totalCount intValue]) {
                [self endLoadMoreMoData];
            }else{
                [self endLoadMore];
            }
        }
        
        if (self.model.page.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopSeparateParam *param = [ShopSeparateParam new];
    param.shopId = @0;
    param.pigId = @0;
    param.pigPositionId = @0;
    param.page = page;
    param.limit = @10;
    [self requestSeparatePlanListByParam:param];
}


#pragma mark - target action
- (void)detailTypeClick:(UIButton *)button {
    button.selected = !button.selected;
    
    if (button.selected) {
        @weakify(self);
        [[PullDownMenu sharedInstance] showWithFrame:CGRectMake(0, TopBarHeight +34, SCREEN_WIDTH, SCREEN_HEIGHT) type:PullDownTypeTransicationTypes dataArr:self.transicationTypeArr completionBlock:^(NSString *text, NSInteger idx ,PullDownType pullWownType) {
            @strongify(self);
            if ([text isEqualToString:@"全部"]) {
                self.transicationType = TransicationTypeAll;
            } else if ([text isEqualToString:@"充值"]){
                self.transicationType = TransicationTypeTopUp;
            } else if ([text isEqualToString:@"养猪"]){
                self.transicationType = TransicationTypeAdoptPig;
            } else if ([text isEqualToString:@"预售款"]){
                self.transicationType = TransicationTypePresell;
            } else if ([text isEqualToString:@"奖金"]){
                self.transicationType = TransicationTypeReward;
            } else if ([text isEqualToString:@"提现"]){
                self.transicationType = TransicationTypeWithdraw;
            } else if ([text isEqualToString:@"买肉"]) {
                self.transicationType = TransicationTypeBugMeat;
            }
            //修改title和img的edge
//            [self.detailTypeBtn setTitle:text forState:UIControlStateNormal];
//            [self changeBtnEdge];
//            self.detailTypeBtn.selected = !self.detailTypeBtn.selected;
//            [self.dataArr removeAllObjects];
//            self.currentPage = 1;
            //[self dataRequest];
        }];
    } else {
        [[PullDownMenu sharedInstance] hide];
    }
}


#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


#pragma mark - tableview.datascource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.model.page.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SeparatePlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeparatePigCell"];

    if (self.model.page.list.count >0) {
        ShopSeparateList *item = self.model.page.list[indexPath.section];
        cell.item = item;
    }
    
    return cell;
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.rowHeight = 118;
    self.tableView.height = KScreenHeight - TJ_NavBarHeight -34;
    self.tableView.mj_y = TJ_NavBarHeight +34;
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[SeparatePlanCell class] forCellReuseIdentifier:@"SeparatePigCell"];
}


- (MeatPayView *)footer{
    
    if (!_footer) {
        _footer = [[MeatPayView alloc] init];
        _footer.type = MeatPayType_SeparatePlan;
        _footer.alpha = 0;
        
        //全选Block
        @weakify(self);
        _footer.chooseCartBlock = ^(UIButton *btn) {

        };
        
        //预留
        _footer.clearCartBlock = ^{
            @strongify(self);
            [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"您确定要全部预留吗？，一经预留不可更改" confirmBtnTitle:@"确定" confirmBlock:^{
               // [self clearLoginAccount];
            } cancelBlock:nil];
        };

        //销售
        _footer.deleteCartBlock = ^{

        };
    }
    return _footer;
}


- (NSMutableArray *)transicationTypeArr{
    
    if (!_transicationTypeArr) {
        _transicationTypeArr = [[NSMutableArray alloc] init];
    }
    return _transicationTypeArr;
}


- (UIButton *)detailTypeBtn {
    
    if (!_detailTypeBtn) {
        _detailTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailTypeBtn.frame = CGRectMake(0, 0, 100, 0);
        [_detailTypeBtn setTitle:@"全部部位" forState:UIControlStateNormal];
        [_detailTypeBtn setTitleColor:UIColorFromRGB(0x262626) forState:UIControlStateNormal];
        _detailTypeBtn.titleLabel.font = mFont(15);
        [_detailTypeBtn setImage:[UIImage imageNamed:@"switch_gray_up"] forState:UIControlStateNormal];
        [_detailTypeBtn setImage:[UIImage imageNamed:@"switch_gray_down"] forState:UIControlStateSelected];
        [_detailTypeBtn addTarget:self action:@selector(detailTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_detailTypeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - _detailTypeBtn.imageView.image.size.width, 0, _detailTypeBtn.imageView.image.size.width)];
        [_detailTypeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _detailTypeBtn.titleLabel.bounds.size.width, 0, -_detailTypeBtn.titleLabel.bounds.size.width)];
    }
    return _detailTypeBtn;
}


- (ShopSeparateModel *)model{
    
    if (!_model) {
        _model = [[ShopSeparateModel alloc] init];
    }
    return _model;
}

#pragma mark override methods

- (void)loadErrorData
{
    [super loadErrorData];
    self.detailTypeBtn.hidden = YES;
    self.footer.hidden = YES;
}

- (void)loadNoData
{
    [super loadNoData];
    self.detailTypeBtn.hidden = YES;
    self.footer.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.detailTypeBtn.hidden = NO;
    self.footer.hidden = NO;
}

- (void)endRefresh{
    [super endRefresh];
    self.detailTypeBtn.hidden = NO;
    self.footer.hidden = NO;
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.detailTypeBtn.hidden = NO;
    self.footer.hidden = NO;
}

@end

